<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>MyStore后台，欢迎您，管理员</title>
  <!-- 引入 Bootstrap 样式 -->
  <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
  <!-- 引入 FontAwesome 图标库 -->
  <link rel="stylesheet" href="https://unpkg.com/dropzone@5.9.3/dist/min/dropzone.min.css" type="text/css"/>
  <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css">
  <link rel="stylesheet" href="css/manage_style.css" />
  <style>
    .container-fluid, .container {
      width: 100%;
    }

    /* 左侧导航区域样式 */
    .nav-side-menu {
      overflow: auto;
    }

    .nav-side-menu .brand {
      background-color: #ff7f00;
      line-height: 70px;
      display: inline-block;
      font-size: 24px;
      padding-left: 15px;
      padding-right: 30px;
      color: #fff;
    }

    .nav-side-menu .brand i {
      margin-right: 10px;
      font-size: 36px;
      top: 8px;
      position: absolute;
    }

    .nav-side-menu ul,
    .nav-side-menu li {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    .nav-side-menu ul :not(collapsed) .arrow:before,
    .nav-side-menu li:not(collapsed) .arrow:before {
      font-family: "FontAwesome";
      content: "\f078";
      display: inline-block;
      margin-right: 10px;
      margin-top: 5px;
      float: right;
    }

    .nav-side-menu ul .active,
    .nav-side-menu li .active {
      border-left: 3px solid #d19b3d;
      background-color: #2a3b46;
    }

    .nav-side-menu ul .sub-menu li.active,
    .nav-side-menu li .sub-menu li.active {
      color: #fff;
    }

    /* 内容区域样式 */
    .content {
      border: 1px solid #d9d9d9;
      background: #f4f4f4;
      height: 100vh;
      padding: 20px;
    }

    .content-title {
      margin-top: 10px;
      margin-bottom: 20px;
      font-size: 28px;
    }

    .content-text {
      font-size: 18px;
    }
    .table {
      table-layout: fixed;
    }

    .table th,
    .table td {
      text-align: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    #pagination {
      position: fixed;
      bottom: 0;
      margin: 0;
      padding: 0;
      width: 100%;
      /* 其他样式属性 */
    }
  </style>
</head>

<body>
<div class="container-fluid">
  <div class="row">
    <!-- 左侧侧边栏 -->
    <div class="col-md-1 bg-dark">
      <div class="py-3">
        <h3 class="text-white text-center mb-4">MyStore</h3>
        <ul class="list-unstyled">
          <li>
            <a th:href="@{/manage/user/page.do}" class="text-white d-flex align-items-center p-3">
              <i class="fas fa-user mr-2"></i>
              <span>用户管理</span>
            </a>
          </li>
          <li>
            <a th:href="@{/manage/role/page.do}" class="text-white d-flex align-items-center p-3"
               aria-disabled="false">
              <i class="fas fa-users-cog mr-2"></i>
              <span>角色管理</span>
            </a>
          </li>
          <li>
            <a th:href="@{/manage/uri/page.do}" class="text-white d-flex align-items-center p-3">
              <i class="fas fa-lock mr-2"></i>
              <span>Uri管理</span>
            </a>
          </li>
          <li>
            <a th:href="@{/manage/app/page.do}" class="text-white d-flex align-items-center p-3">
              <i class="fab fa-shopify mr-2"></i>
              <span>应用管理</span>
            </a>
          </li>
          <li>
            <a th:href="@{/manage/app/onlinePage.do}" class="text-white d-flex align-items-center p-3">
              <i class="fas fa-arrow-circle-up mr-2"></i>
              <span>应用上架</span>
            </a>
          </li>
          <li>
            <a th:href="@{/manage/app/offlinePage.do}" class="text-white d-flex align-items-center p-3">
              <i class="fas fa-arrow-circle-down mr-2"></i>
              <span>应用下架</span>
            </a>
          </li>
          <li>
            <a th:href="@{/manage/app/updateAppVersion.do}" class="text-white d-flex align-items-center p-3">
              <i class="fas fa-cloud-upload-alt mr-2"></i>
              <span>更新应用</span>
            </a>
          </li>
        </ul>
      </div>
    </div>
    <div class="col-md-11 content">
      <h2 class="content-title">应用信息</h2>
      <form th:action="@{/manage/app/save.do}" method="POST" id="appForm">
        <div class="form-group">
          <label for="appName">应用名</label>
          <input type="hidden" name="appId" th:value="${app?.appId}">
          <input type="hidden" name="mode" th:value="${mode}">
          <input type="text" class="form-control" id="appName" th:value="${app?.appName}" name="appName" required>
        </div>
        <div class="form-group">
          <label for="appDescription">应用描述</label>
          <input type="text" class="form-control" id="appDescription" th:value="${app?.appDescription}" name="appDescription" required>
        </div>
        <div class="form-group">
          <label for="appIcon" >图标</label>
          <input type="hidden" id="appIcon" name="appIcon" required>
          <div id="appIconPreview" class="dropzone"></div>
        </div>
        <div class="form-group">
          <label th:for="appImage">截图</label>
          <input type="hidden" id="appImage" name="appImage" required>
          <div id="appImagePreview" class="dropzone"></div>
        </div>
        <div class="form-group">
          <label for="appState">是否上架</label>
          <select class="form-control" id="appState" name="appState" required>
            <option value="0">上架</option>
            <option value="1">下架</option>
          </select>
        </div>
        <button type="submit" class="btn btn-primary">提交</button>
      </form>
    </div>

  </div>
</div>
<script th:inline="javascript">
  // 在这里编写JavaScript代码
  var message = [[${result}]];
  if (message!==undefined&&message!==null&&message!=='')
    alert(message==='true'?'操作成功':'操作失败');
</script>
<!-- 引入 jQuery 和 Bootstrap JavaScript -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.9.3/min/dropzone.min.js"></script>

<script th:inline="javascript">
  var isLoad=true;
  var index=0;
  var appIcon=[[${app?.appIcon}]];
  var ids=[[${app?.getImages()}]];
  if(ids===null||ids===undefined)
    isLoad=false;
  // 初始化appIcon上传工件
  var appImagePreview = new Dropzone("#appImagePreview", {
    url: "/manage/fileModel/upload.do", // 后端上传处理接口
    method:"post",
    acceptedFiles: "image/*", // 限制文件类型为图片
    maxFiles: 3,
    dictDefaultMessage:'在这里放置文件并上传',
    dictRemoveFile:'移除',
    addRemoveLinks: true,// 最多只能上传1个文件
    init: function() {
      this.on("addedfile", function(file) {
        var previewElement = file.previewElement;
        if(isLoad){
          file.accepted=true;
          appImagePreview.files.push(file);
          previewElement.setAttribute("app-image-id",ids[index]+"");
        }
      });

    },
    success: function(file, response) {
      if (file.previewElement) {
        var fileModelId=response.result[0];
        file.previewElement.setAttribute("app-image-id",fileModelId+"");
        return file.previewElement.classList.add("dz-success");
      }

    }
  });
  if(isLoad){
    for(;index<ids.length;index++)
      appImagePreview.displayExistingFile({name:"图片"}, '/console/file/image.do?fileModelId='+ids[index]);
    isLoad=false;
  }
  var appIconLoad=true;
  if(appIcon===null||appIcon===undefined)
    appIconLoad=false;
  var appIconPreview = new Dropzone("#appIconPreview", {
    url: "/manage/fileModel/upload.do", // 后端上传处理接口
    method:"post",
    acceptedFiles: "image/*", // 限制文件类型为图片
    maxFiles: 1,
    dictDefaultMessage:'在这里放置文件并上传',
    dictRemoveFile:'移除',
    addRemoveLinks: true,// 最多只能上传1个文件
    init: function() {
      this.on("addedfile", function(file) {
        var previewElement = file.previewElement;
        if(appIconLoad){
          file.accepted=true;
          appIconPreview.files.push(file);
          previewElement.setAttribute("app-icon-id",appIcon+"");
        }
      });

    },
    success: function(file, response) {
      if (file.previewElement) {
        var fileModelId=response.result[0];
        file.previewElement.setAttribute("app-icon-id",fileModelId+"");
        return file.previewElement.classList.add("dz-success");
      }

    }
  });
  if(appIconLoad){
    appIconPreview.displayExistingFile({name:"图片"}, '/console/file/image.do?fileModelId='+appIcon);
    appIconLoad=false;
  }
  // 监听表单提交事件
  $("#appForm").submit(function() {
    // 取出上传工件的数据并拼接成字符串形式
    $("#appIcon").val($('div[app-icon-id]').attr('app-icon-id'));
    var appImage="";
    $('div[app-image-id]').each(function (){
      var imageId=$(this).attr('app-image-id');
      if(imageId!==null&&imageId!==undefined)
        appImage+=imageId+",";
    });
    $("#appImage").val(appImage.substring(0,appImage.length-1));
  });
</script>
</body>

</html>